﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CRM.DataAccess.Entities;
using CRM.Business.Services;
using CRM.Encryption;
using Telerik.Web.UI;

namespace Web.Company
{
    public partial class AddNewCompany : System.Web.UI.Page
    {
        static long? assignedToID = null;
        static string prevPage = String.Empty;
        static USER user;
        static ROLE r;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;
            if (Request.UrlReferrer != null)
            {
                prevPage = Request.UrlReferrer.ToString();
                hdntxtPrevPage.Value = prevPage;
            }
            assignedToID = null;
            user = (USER)Session["User"];
            if (user != null)
            {
                r = RoleService.GetRoleByID((long)user.ROLE_ID);
                if ((!(bool)user.IS_ADMIN) && (!(bool)r.IS_MANAGER))
                {
                    assignedToID = user.ID;
                    divAssignedTo.Visible = false;
                }
                txtCompanyWebsite.Attributes.Add("onblur", "CheckNull();");
                txtPrimaryEmail.Attributes.Add("onblur", "CheckNull();");
                txtSecondaryEmail.Attributes.Add("onblur", "CheckNull();");
                LoadOrgIndustry();
                LoadOrgRating();
                LoadOrgType();
                LoadAllUser();

            }
        }
        private void LoadOrgType()
        {
            ddlType.DataSource = OrganizationService.GetOrganizationType();
            ddlType.DataBind();
            ddlType.Items.Insert(0, new RadComboBoxItem("Select an option", "-1"));
        }
        private void LoadOrgRating()
        {
            ddlRating.DataSource = OrganizationService.GetOrganizationRating();
            ddlRating.DataBind();
            ddlRating.Items.Insert(0, new RadComboBoxItem("Select an option", "-1"));
        }
        private void LoadOrgIndustry()
        {
            DdlIndustry.DataSource = OrganizationService.GetOrganizationIndustry();
            DdlIndustry.DataBind();
            DdlIndustry.Items.Insert(0, new RadComboBoxItem("Select an option", "-1"));
        }
        private void LoadAllUser()
        {
            ddlAssignedTo.Items.Clear();
            List<USER> users = UserService.ListAll();
            if (users == null)
            {
                return;
            }
            else
            {
                foreach (USER u in users)
                {
                    ddlAssignedTo.Items.Add(new RadComboBoxItem(u.SUR_NAME + " " + u.NAME, u.ID.ToString()));
                }
            }
        }
        private int addNewOrganization()
        {
            ORGANIZATION org = new ORGANIZATION();
            DateTime dt = DateTime.Now;
            org.CREATED_BY = 1;//check
            org.DATE_ENTERED = dt;
            org.DATE_MODIFIED = dt;
            // ORGANIZATION DETAILS
            org.ORGANIZATION_NAME = txtOrganizationName.Text;
            org.COMPANY_WEBSITE = txtCompanyWebsite.Text;
            org.CONTACT_NUMBER = txtContactNumber.Text;
            org.TICKER_SYMBOL = txtTickerSymbol.Text;
            org.FAX = txtFax.Text;
            if (hdntxtOrgID.Value != "")
                org.MEMBER_OF_ORG_ID = Convert.ToInt64(hdntxtOrgID.Value);
            else
                org.MEMBER_OF_ORG_ID = null;
            org.PHONE_SECONDARY = txtSecondaryPhone.Text;
            org.EMPLOYEE = txtEmployees.Text;
            org.PRIMARY_EMAIL = txtPrimaryEmail.Text;
            org.SECONDARY_EMAIL = txtSecondaryEmail.Text;
            org.OWNERSHIP = txtOwnership.Text;
            if (DdlIndustry.SelectedValue != "-1")
                org.INDUSTRY_ID = Convert.ToInt64(DdlIndustry.SelectedValue);
            else
                org.INDUSTRY_ID = null;
            if (ddlRating.SelectedValue != "-1")
                org.RATING_ID = Convert.ToInt64(ddlRating.SelectedValue);
            else
                org.RATING_ID = null;
            if (ddlType.SelectedValue != "-1")
                org.TYPE_ID = Convert.ToInt64(ddlType.SelectedValue);
            else
                org.TYPE_ID = null;

            if ((!(bool)user.IS_ADMIN) && (!(bool)r.IS_MANAGER))
                org.ASSIGN_TO = user.ID;
            else
            {
                if (ddlAssignedTo.SelectedItem == null)
                    org.ASSIGN_TO = null;
                else
                    org.ASSIGN_TO = Convert.ToInt64(ddlAssignedTo.SelectedValue);
            }
            org.COMPANY_REGISTRATION_NUMBER = txtCompanyRegistrationNumber.Text;
            // ADDRESS DETAILS
            org.OFFICE_ADDRESS = txtOfficeAddress.Text;
            org.HOME_ADDRESS = txtHomeAddress.Text;
            org.OFFICE_PO_BOX = txtOfficePOBox.Text;
            org.HOME_PO_BOX = txtHomePOBox.Text;
            org.OFFICE_CITY = txtOfficeCity.Text;
            org.HOME_CITY = txtHomeCity.Text;
            org.OFFICE_STATE = txtOfficeState.Text;
            org.HOME_STATE = txtHomeState.Text;
            org.OFFICE_POSTAL_CODE = txtOfficePostalCode.Text;
            org.HOME_POSTAL_CODE = txtHomePostalCode.Text;
            org.OFFICE_COUNTRY = txtOfficeCountry.Text;
            org.HOME_COUNTRY = txtHomeCountry.Text;

            return OrganizationService.AddNewOrganization(org);
        }
        protected void btn_Cancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(prevPage);
        }
        protected void btn_Save_Click(object sender, EventArgs e)
        {
            int newOrganization = addNewOrganization();
            if (newOrganization == 1)
            {
                RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"ShowMessageAndRedirect('" + "Add new company successful !" + "','" + "Companies.aspx" + "');");
                //MessageBox.Show(this.Page, Page.GetType(), "Add new Company successful !", "Companies.aspx");
            }
            else
                RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"ShowMessage('" + "Can not add new company !" + "');");
                //ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "alert('Can not add new Company !');", true);
        }
    }
}